locals {
  ecs_groups = {
    ecs_devops = {
      ecs_name      = ["ecs-dev-Devops-001"]
      image_id      = data.huaweicloud_images_image.ecs_image_centos_79.id
      flavor_id     = data.huaweicloud_compute_flavors.ecs_4c16g.ids[0]
      key_pair      = data.terraform_remote_state.keypair.outputs.keypairs_info.id
      ava_zone      = "${var.REGION}c"
      vsg_ids       = data.terraform_remote_state.vsg.outputs.vsg_info.vsg_devops.vsg_id
      subnet_id     = data.terraform_remote_state.subnet.outputs.subnet_info["subnet-me212-dev-baidu_hub-devops"].id
      eip_id        = data.terraform_remote_state.eip.outputs.eip_info["eip_ecs_devops-dev"].id
      sys_disk_type = "SSD"
      sys_disk_size = 500
    }
    ecs_web = {
      ecs_name      = ["ecs-dev-web-001", "ecs-dev-web-002", "ecs-dev-web-003"]
      image_id      = data.huaweicloud_images_image.ecs_image_centos_79.id
      flavor_id     = data.huaweicloud_compute_flavors.ecs_4c8g.ids[0]
      key_pair      = data.terraform_remote_state.keypair.outputs.keypairs_info.id
      ava_zone      = "${var.REGION}c"
      vsg_ids       = data.terraform_remote_state.vsg.outputs.vsg_info.vsg_web.vsg_id
      subnet_id     = data.terraform_remote_state.subnet.outputs.subnet_info["subnet-me212-dev-baidu_hub-ecs-web"].id
      eip_id        = ""
      sys_disk_type = "SSD"
      sys_disk_size = 200
    }
    ecs_voip = {
      ecs_name      = ["ecs-dev-voip-001", "ecs-dev-voip-002", "ecs-dev-voip-003"]
      image_id      = data.huaweicloud_images_image.ecs_image_centos_79.id
      flavor_id     = data.huaweicloud_compute_flavors.ecs_4c16g.ids[0]
      key_pair      = data.terraform_remote_state.keypair.outputs.keypairs_info.id
      ava_zone      = "${var.REGION}c"
      vsg_ids       = data.terraform_remote_state.vsg.outputs.vsg_info.vsg_voip.vsg_id
      subnet_id     = data.terraform_remote_state.subnet.outputs.subnet_info["subnet-me212-dev-baidu_hub-ecs-voip"].id
      eip_id        = ""
      sys_disk_type = "SSD"
      sys_disk_size = 500
    }
    ecs_miniapp = {
      ecs_name      = ["ecs-dev-miniapp-001", "ecs-dev-miniapp-002", "ecs-dev-miniapp-003"]
      image_id      = data.huaweicloud_images_image.ecs_image_centos_79.id
      flavor_id     = data.huaweicloud_compute_flavors.ecs_4c16g.ids[0]
      key_pair      = data.terraform_remote_state.keypair.outputs.keypairs_info.id
      ava_zone      = "${var.REGION}c"
      vsg_ids       = data.terraform_remote_state.vsg.outputs.vsg_info.vsg_miniapp.vsg_id
      subnet_id     = data.terraform_remote_state.subnet.outputs.subnet_info["subnet-me212-dev-baidu_hub-ecs-miniapp"].id
      eip_id        = ""
      sys_disk_type = "SSD"
      sys_disk_size = 500
    }
  }

  flattened_ecs_groups = flatten([
    for group, config in local.ecs_groups : [
      for i, ecs_name in config.ecs_name : {
        group         = group
        ecs_name      = ecs_name
        image_id      = config.image_id
        flavor_id     = config.flavor_id
        key_pair      = config.key_pair
        ava_zone      = config.ava_zone
        vsg_ids       = config.vsg_ids
        subnet_id     = config.subnet_id
        eip_id        = config.eip_id
        sys_disk_type = config.sys_disk_type
        sys_disk_size = config.sys_disk_size
      }
    ]
  ])
}

module "ecs" {
  source    = "../../../modules/ecs"
  for_each  = { for instance in local.flattened_ecs_groups : "${instance.group}-${instance.ecs_name}" => instance }
  ecs_name  = each.value.ecs_name
  image_id  = each.value.image_id
  flavor_id = each.value.flavor_id
  key_pair  = each.value.key_pair
  ava_zone  = each.value.ava_zone
  vsg_ids   = each.value.vsg_ids
  subnet_id = each.value.subnet_id
  user_data = <<-EOF
                    echo "StrictHostKeyChecking no" >> /etc/ssh/ssh_config
                EOF
  eip_id    = each.value.eip_id
  disk_type = each.value.sys_disk_type
  disk_size = each.value.sys_disk_size
}